package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    // 分页查询所有公寓的所有房间信息 + 检索条件
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    // 查询房间状态
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    // 通过房间id 查询房间的详细信息
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {

        return roomInfoMapper.pageRoomItemByQuery(roomItemVoPage, queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        // 房间基本信息
        RoomInfo roomInfo = this.getById(id);
        // 相同属性对拷
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        // 房间的其他相关
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectByApartmentId(roomInfo.getApartmentId());
        List<GraphInfo> graphVoList = graphInfoMapper.selectGraphListByItemIdAndItemType(id, ItemType.ROOM);
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrListByRoomId(id);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityListByRoomId(id);
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelListByRoomId(id);
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentTypeByRoomId(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeListByApartmentId(roomInfo.getApartmentId());
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermByRoomId(id);
        // 房间是否被删除(房间基本信息)
        Boolean isDelete = roomInfo.getIsDeleted() == 1;
        // 房间是否被入住(租约表leaseAgreement 的状态是否是2,5 使用通用leaseAgreement 的mapper方法) 为true表示入住
        Long roomCount = leaseAgreementMapper.selectCount(new LambdaQueryWrapper<LeaseAgreement>()
                .eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus, 2, 5));
        Boolean isCheckIn = roomCount > 0;

        // 房间相关属性的赋值
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsDelete(isDelete);
        roomDetailVo.setIsCheckIn(isCheckIn);

        // 保存浏览记录
        // 后续会增加 不同时间访问同一个房间更新浏览时间的逻辑操作 所有需要在browsingHistoryService增编写逻辑代码
        browsingHistoryService.saveHistory(LoginUserContext.getLoginUser().getUserId(), id);

        return roomDetailVo;
    }

    // 分页查询所属公寓的所有房间信息 + 检索条件
    @Override
    public IPage<RoomItemVo> pageRoomItemByApartmentId(Page<RoomItemVo> roomItemVoPage, Long id) {
        return roomInfoMapper.pageRoomItemByApartmentId(roomItemVoPage,id);
    }
}




